#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include "server.h"

int chat_to_all(int sock_fd)
{
	int tmp;
	char from[NAME_LEN_MAX + 1];
	char msg[MSG_LEN_MAX + 1];
	
	recv(sock_fd, &tmp, sizeof(int), 0);
	memset(from, 0, NAME_LEN_MAX + 1);
	recv(sock_fd, from, tmp, 0);
	
	recv(sock_fd, &tmp, sizeof(int), 0);
	memset(msg, 0, MSG_LEN_MAX + 1);
	recv(sock_fd, msg, tmp, 0);

	int i;
	for (i = 0; (i < USER_NUM_MAX) && (strlen(users[i].name) > 0); i++) 
	{
    		if ( (users[i].sock_fd != -1) && (strcmp(users[i].name, from) != 0))
		{
			tmp = strlen(from);
   	         	send(users[i].sock_fd2, &tmp, sizeof(int), 0);
			send(users[i].sock_fd2, from, tmp, 0);
			tmp = strlen(msg);
   	         	send(users[i].sock_fd2, &tmp, sizeof(int), 0);
			send(users[i].sock_fd2, msg, tmp, 0);
		}
	}
	tmp = 0;
	send(sock_fd, &tmp, sizeof(int), 0);
}
